﻿using System;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core.Oracle
{
    sealed class OracleParameterRenderer : IParameterRenderer
    {
        #region RenderParameterName - parameter names are prefixed by ':' character.

        public string RenderParameterName(DbParameter parameter)
        {
            return ":" + parameter.ParameterName;
        }

        #endregion

        #region GetUniqueName - index is appended to shortened original/initial parameter name since length of variable name in Oracle may not exceed 30 characters.

        // Parameter names generated by BizElements are often to long because table and column names, ie. prefixed aliases, are used to build them.
        // Max variable name length in Oracle is 30 characters. BizElements will support up to 9999 parameters in collection for Oracle.
        // 4 digits and underscore character are appended to shortened name.
        // An error will occur if collection contains more than 10000 parameters.
        const int MaxOriginalNameLen = 25;

        public string GetUniqueName(string originalParameterName, int parameterIndexInCollection)
        {
            string uniqueName;
            bool isToLong = (originalParameterName.Length > MaxOriginalNameLen);
            if (isToLong)
            {
                string shortenedName = originalParameterName.Substring(0, MaxOriginalNameLen);
                uniqueName = shortenedName + "_" + parameterIndexInCollection;
            }
            else
            {
                uniqueName = originalParameterName + "_" + parameterIndexInCollection;
            }

            return uniqueName;
        }

        #endregion
    }
}